package com.chinaedu.oc.controller.saledate;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinaedu.oc.constant.Constant;
import com.chinaedu.oc.po.commodity.dto.CommodityBaseDto;
import com.chinaedu.oc.po.commodity.dto.CommodityListBaseDto;
import com.chinaedu.oc.po.dto.PubOutputDto;
import com.chinaedu.oc.po.saledate.Saledate;
import com.chinaedu.oc.po.saledate.dto.SaledateInput;
import com.chinaedu.oc.serv.saledate.ISaledateServ;
import com.chinaedu.oc.util.common.JsonUtil;

/**
 *
 * @author Administrator
 * 销售日期信息
 */
@Controller
@RequestMapping("/saledate/")
public class SaledateController {


	@Autowired
	private ISaledateServ iSaledateServ;

	/**
	 *  添加或修改销售日期
	 *  参数:  SaledateInput  入参数据详见SaledateInput实体类
	 * @param saledates   销售日期集合
	 * @param commodityId   商品id
	 */
	@RequestMapping("insertOrUpdate")
	public @ResponseBody PubOutputDto insertOrUpdate(String data) {
		SaledateInput saledateInput = JsonUtil.toBean(data, SaledateInput.class);
		String commodityId = saledateInput.getCommodityId();
		List<Saledate> saledates = saledateInput.getSaledates();
		PubOutputDto pubOutputDto = new PubOutputDto();
		try {
			iSaledateServ.insertOrUpdate(saledates, commodityId);
			pubOutputDto.setStatus(Constant.TRUE);
			pubOutputDto.setMsg("添加或修改成功");
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			pubOutputDto.setStatus(Constant.FALSE);
			pubOutputDto.setMsg("添加或修改失败");
		}
		return pubOutputDto;
	}

	/**
	 *  添加或修改销售日期描述
	 *  参数：  saledate   销售日期实体
	 * @param saledates   销售日期集合
	 * @param commodityId   商品id
	 */
	@RequestMapping("insertOrUpdateMsg")
	public  @ResponseBody PubOutputDto  insertOrUpdateMsg(String data) {
		Saledate saledate = JsonUtil.toBean(data, Saledate.class);
		PubOutputDto pubOutputDto = new PubOutputDto();
		try {
			iSaledateServ.insertOrUpdateMsg(saledate);
			pubOutputDto.setStatus(Constant.TRUE);
			pubOutputDto.setMsg("添加或修改成功");
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			pubOutputDto.setStatus(Constant.FALSE);
			pubOutputDto.setMsg("添加或修改失败");
		}
		return pubOutputDto;
	}

	/**
	 * 查询销售日期列表
	 * 参数:   commodityId    商品ID
	 * @return
	 */
	@RequestMapping("findSaledate")
	public @ResponseBody CommodityListBaseDto<Saledate> findSaledate(String data) {
		Map map = JsonUtil.toBean(data, Map.class);
		String  commodityId = (String)map.get("commodityId");
		CommodityListBaseDto<Saledate> commodityListBaseDto = new CommodityListBaseDto<Saledate>();
		try {
			commodityListBaseDto.setData(iSaledateServ.findSaledateByCommodityId(commodityId));
			commodityListBaseDto.setStatus(Constant.TRUE);
			commodityListBaseDto.setMsg("查询成功");
		}catch (Exception e) {
			// TODO: handle exception
		}
		return commodityListBaseDto;
	}

	/**
	 * 查询销售日期内描述 ，图片， 视频信息
	 * 参数:   commodityId    商品ID
	 * @return
	 */
	@RequestMapping("findBysaleId")
	public @ResponseBody CommodityBaseDto<Saledate> findBysaleId(String data) {
		Map map = JsonUtil.toBean(data, Map.class);
		String  saleId = (String)map.get("saledateId");
		CommodityBaseDto<Saledate> commodityBaseDto = new CommodityBaseDto<Saledate>();
		try {
			commodityBaseDto.setData(iSaledateServ.queryBySaleId(saleId));
			commodityBaseDto.setStatus(Constant.TRUE);
			commodityBaseDto.setMsg("查询成功");
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			commodityBaseDto.setStatus(Constant.FALSE);
			commodityBaseDto.setMsg("查询失败");
		}
		return commodityBaseDto;
	}

	/**
	 *
	 * @param data
	 * @return
	 */
	@RequestMapping("findSaledateByDate")
	public @ResponseBody CommodityBaseDto<Saledate> findSaledateByDate(String data) {
		String commodityId = JsonUtil.toBean(data, String.class);
		CommodityBaseDto<Saledate> commodityBaseDto = new CommodityBaseDto<Saledate>();
		try {
			Saledate saledate = new Saledate();
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			saledate.setCurrentDate(ts);
			saledate.setCommodityId(commodityId);
			List<Saledate> saledates = iSaledateServ.findSaledateByCase(saledate);
			if(saledates.size() > 0) {
				commodityBaseDto.setData(saledates.get(0));
			}else {
				commodityBaseDto.setData(null);
			}
			commodityBaseDto.setStatus(Constant.TRUE);
			commodityBaseDto.setMsg("查询成功");
		}catch (Exception e) {
			// TODO: handle exception
			commodityBaseDto.setStatus(Constant.FALSE);
			commodityBaseDto.setMsg("查询失败");
		}
		return commodityBaseDto;
	}

}
